<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="appid" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入appid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入渠道名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="concatMobile">
        <el-input
          v-model="queryParams.concatMobile"
          placeholder="请输入联系人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="concatName">
        <el-input
          v-model="queryParams.concatName"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="请选择启用/禁用" clearable size="small">
          <el-option
            v-for="dict in isEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="免检业务" prop="mjEnable">
        <el-select v-model="queryParams.mjEnable" placeholder="请选择免检业务" clearable size="small">
          <el-option
            v-for="dict in mjEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="代驾业务" prop="djEnable">
        <el-select v-model="queryParams.djEnable" placeholder="请选择代驾业务" clearable size="small">
          <el-option
            v-for="dict in djEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="自驾业务" prop="zjEnable">
        <el-select v-model="queryParams.zjEnable" placeholder="请选择自驾业务" clearable size="small">
          <el-option
            v-for="dict in zjEnableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['third:partner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['third:partner:edit']"
        >修改</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-card class="box-card">
            <div class="text item">
              <el-row :gutter="24" class="expand-item">
                <el-col :span="12">签名秘钥：{{props.row.signKey}}</el-col>
                <el-col :span="12">回调地址：{{props.row.notifyUrl}}</el-col>
              </el-row>
            </div>
          </el-card>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="appid" align="center" prop="appid" />
      <el-table-column label="渠道名称" align="center" prop="name" />
      <el-table-column label="联系人手机号" align="center" prop="concatMobile" />
      <el-table-column label="联系人" align="center" prop="concatName" />
      <el-table-column label="启用/禁用" align="center" prop="isEnable" :formatter="isEnableFormat" />
      <el-table-column label="免检价格" align="center" prop="mjPrice" />
      <el-table-column label="代驾价格" align="center" prop="djPrice" />
      <el-table-column label="免检业务" align="center" prop="mjEnable" :formatter="mjEnableFormat" />
      <el-table-column label="代驾业务" align="center" prop="djEnable" :formatter="djEnableFormat" />
      <el-table-column label="自驾业务" align="center" prop="zjEnable" :formatter="zjEnableFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['third:partner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['third:partner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改渠道合伙人对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-dialogdrag width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="appid" prop="appid" v-if="form.id">
          <el-input v-model="form.appid" placeholder="请输入appid" disabled/>
        </el-form-item>
        <el-form-item label="渠道名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入渠道名称" />
        </el-form-item>
        <el-form-item label="联系人手机号" prop="concatMobile">
          <el-input v-model="form.concatMobile" placeholder="请输入联系人手机号" />
        </el-form-item>
        <el-form-item label="联系人" prop="concatName">
          <el-input v-model="form.concatName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="启用/禁用">
          <el-select v-model="form.isEnable" placeholder="请选择启用/禁用">
            <el-option
              v-for="dict in isEnableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回调地址" prop="notifyUrl">
          <el-input v-model="form.notifyUrl" placeholder="请输入回调地址" />
        </el-form-item>
        <el-form-item label="免检价格" prop="mjPrice">
          <el-input-number v-model="form.mjPrice" placeholder="请输入免检价格"  :step="1" :min="0"
                           :max="400"/>
        </el-form-item>
        <el-form-item label="代驾价格" prop="djPrice">
          <el-input-number v-model="form.djPrice" placeholder="请输入代驾价格"  :step="1" :min="0"
                           :max="400"/>
        </el-form-item>
        <el-form-item label="免检业务">
          <el-select v-model="form.mjEnable" placeholder="请选择免检业务">
            <el-option
              v-for="dict in mjEnableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代驾业务">
          <el-select v-model="form.djEnable" placeholder="请选择代驾业务">
            <el-option
              v-for="dict in djEnableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自驾业务">
          <el-select v-model="form.zjEnable" placeholder="请选择自驾业务">
            <el-option
              v-for="dict in zjEnableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartner, getPartner, delPartner, addPartner, updatePartner, exportPartner } from "@/api/third/partner";

export default {
  name: "Partner",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 渠道合伙人表格数据
      partnerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 启用/禁用字典
      isEnableOptions: [],
      // 免检业务字典
      mjEnableOptions: [],
      // 代驾业务字典
      djEnableOptions: [],
      // 自驾业务字典
      zjEnableOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appid: undefined,
        name: undefined,
        concatMobile: undefined,
        concatName: undefined,
        password: undefined,
        isEnable: undefined,
        signKey: undefined,
        notifyUrl: undefined,
        mjPrice: undefined,
        djPrice: undefined,
        mjEnable: undefined,
        djEnable: undefined,
        zjEnable: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_is_enable").then(response => {
      this.isEnableOptions = response.data;
    });
    this.getDicts("sys_is_enable").then(response => {
      this.mjEnableOptions = response.data;
    });
    this.getDicts("sys_is_enable").then(response => {
      this.djEnableOptions = response.data;
    });
    this.getDicts("sys_is_enable").then(response => {
      this.zjEnableOptions = response.data;
    });
  },
  methods: {
    /** 查询渠道合伙人列表 */
    getList() {
      this.loading = true;
      listPartner(this.queryParams).then(response => {
        this.partnerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 启用/禁用字典翻译
    isEnableFormat(row, column) {
      return this.selectDictLabel(this.isEnableOptions, row.isEnable);
    },
    // 免检业务字典翻译
    mjEnableFormat(row, column) {
      return this.selectDictLabel(this.mjEnableOptions, row.mjEnable);
    },
    // 代驾业务字典翻译
    djEnableFormat(row, column) {
      return this.selectDictLabel(this.djEnableOptions, row.djEnable);
    },
    // 自驾业务字典翻译
    zjEnableFormat(row, column) {
      return this.selectDictLabel(this.zjEnableOptions, row.zjEnable);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        appid: undefined,
        name: undefined,
        concatMobile: undefined,
        concatName: undefined,
        password: undefined,
        isEnable: undefined,
        signKey: undefined,
        notifyUrl: undefined,
        mjPrice: undefined,
        djPrice: undefined,
        mjEnable: undefined,
        djEnable: undefined,
        zjEnable: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道合伙人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPartner(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改渠道合伙人";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePartner(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addPartner(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除渠道合伙人编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPartner(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有渠道合伙人数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPartner(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
